<template>
  <div>
    <div class="form">
      <div class="search">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="medium"
          :inline="true"
        >
          <el-form-item label="设备选择" prop="device">
            <el-select
              v-model="queryParams.deviceIds"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in deviceList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="能耗" prop="energy">
            <el-select
              v-model="queryParams.energyType"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in energyList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="统计方式" prop="way">
            <el-select
              v-model="queryParams.timeDimension"
              placeholder="请选择"
              @change="changeTimeDimension"
              style="width: 100%"
            >
              <el-option
                v-for="item in wayList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选择时间" prop="time">
            <el-date-picker
              v-model="queryParams.time"
              v-if="queryParams.timeDimension === 'DAY'"
              @change="changeTime"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              range-separator="至"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
            <el-date-picker
              v-model="queryParams.time"
              v-if="queryParams.timeDimension === 'MONTH'"
              type="monthrange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              range-separator="至"
              value-format="yyyy-MM"
            >
            </el-date-picker>
            <el-date-picker
              v-model="queryParams.time"
              v-if="queryParams.timeDimension === 'YEAR'"
              type="years"
              placeholder="请选择"
              value-format="yyyy"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div>
          <el-button type="primary" @click="getList()" class="searchBtn"
            >搜索</el-button
          >
          <el-button @click="reset()" class="searchBtn">重置</el-button>
        </div>
      </div>
      <div id="chart" style="width: 100%; height: 700px"></div>
    </div>
  </div>
</template>

<script>
import {
  ledgerList,
  ledgerAdd,
  ledgerUpdate,
  ledgerDel,
  ledgerDetail,
  standardList,
  standardAdd,
  standardUpdate,
  standardDel,
  standardDetail,
  realtimeAnalysis,
} from "@/api/system/device";
import {} from "@/api/article";
import echarts from "echarts";
import moment from "moment";
import { getToken } from "@/utils/auth";
export default {
  components: {},
  filters: {
    statusList: function (value) {
      switch (value) {
        case "1":
          return "已提交";
        case "2":
          return "已分派";
        case "3":
          return "已解决";
      }
    },
  },
  data() {
    return {
      form: {},
      queryParams: {
        timeDimension: "DAY",
        energyType: "1",
        deviceIds: "JDL_jiyaji1_DB",
        time: [
          moment().add("days", -7).format("YYYY-MM-DD"),
          moment().format("YYYY-MM-DD"),
        ],
      },
      color: ["#4f8ae3", "#16bf16", "#ff8f2c", "#cf1a1a"],
      rules: {
        deviceName: [{ required: true, message: "请输入", trigger: "blur" }],
        deviceId: [{ required: true, message: "请输入", trigger: "blur" }],
        deviceModule: [
          { required: true, message: "请输入", trigger: "change" },
        ],
      },
      wayList: [
        { value: "DAY", label: "日度" },
        { value: "MONTH", label: "月度" },
        // { value: "YEAR", label: "年度" },
      ],
      energyList: [
        { value: "1", label: "总量" },
        { value: "2", label: "甲班和乙班" },
      ],
      deviceList: [
        { value: "JDL_jiyaji1_DB", label: "挤压机1" },
        { value: "JDL_jiyaji2_DB", label: "挤压机2" },
        { value: "JDL_jiyaji3_DB", label: "挤压机3" },
        { value: "JDL_jiyaji4_DB", label: "挤压机4" },
        { value: "JDL_jiyaji5_DB", label: "挤压机5" },
        { value: "JDL_shixiaolu1_DB", label: "时效炉挤压机1" },
      ],
      totalData: [
        { name: "总用电能耗", number: "1000" },
        { name: "峰值总能耗", number: "1000" },
        { name: "平值总能耗", number: "1000" },
        { name: "谷值总能耗", number: "1000" },
      ],
      type: "",
      uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      token: {
        Authorization: getToken(),
      },
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      modelClass: null,
      visible: false,
      list: [],
      listLoading: false,
      total: 0,
      limit: 10,
      curPage: 1,
      listData: [],
      studentName: "",
      // studentClass: '',
      phonenumber: "",
      work: "",
      page: this.$route.query.page,
      personClass: "",
      classList: [],
      userId: null,
      imageUrl: "",

      imageName: null,
      title: "",
      timeAxis: [],
      series: {},
    };
  },
  created() {},
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      let params = {
        deviceIds: [this.queryParams.deviceIds],
        timeDimension: this.queryParams.timeDimension,
        energyType: this.queryParams.energyType,
      };
      if (this.queryParams.time.length > 0) {
        if (this.queryParams.timeDimension == "DAY") {
          params.startTime = moment(this.queryParams.time[0]).format(
            "YYYY-MM-DD"
          );
          params.endTime = moment(this.queryParams.time[1]).format(
            "YYYY-MM-DD"
          );
        } else if (this.queryParams.timeDimension == "MONTH") {
          params.startTime = moment(this.queryParams.time[0]).format(
            "YYYY-MM"
          );
          params.endTime = moment(this.queryParams.time[1]).format(
            "YYYY-MM"
          );
        } else if (this.queryParams.timeDimension == "YEAR") {
          console.log(this.queryParams.time)
        }
      }
      realtimeAnalysis(params).then((res) => {
        this.timeAxis = res.data.timeAxis;
        this.series = res.data.series[0];
        this.drawLine();
      });
    },
    changeTime() {
      const start = new Date(this.queryParams.time[0]);
      const end = new Date(this.queryParams.time[1]);
      const diffDays = Math.floor((end - start) / (1000 * 60 * 60 * 24));
      if (diffDays > 60) {
        this.$message.error("时间间隔不能超过60天");
        this.queryParams.time = [];
        return;
      }
    },
    changeTimeDimension() {
      this.queryParams.time = [];
    },
    drawLine() {
      let series = [];
      let unit = "";
      if (this.queryParams.energyType == 1) {
        unit = "度";
        series = [
          {
            name: "总量",
            type: "bar",
            barWidth: "20px",
            data: this.series.totalData,
          },
        ];
      } else {
        unit = "度";
        series = [
          {
            name: "甲班",
            type: "bar",
            barWidth: "20px",
            data: this.series.dayShiftData,
          },
          {
            name: "乙班",
            type: "bar",
            barWidth: "20px",
            data: this.series.nightShiftData,
          },
        ];
      }
      this.chartLine = echarts.init(document.getElementById("chart"));
      let _this = this;
      // 指定图表的配置项和数据
      var option = {
        legend: {
          data: ["甲班", "乙班"],
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: _this.timeAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            name: unit,
            type: "value",
          },
        ],
        series: series,
      };

      // 使用刚指定的配置项和数据显示图表。
      this.chartLine.setOption(option, true);
    },
  },
};
</script>
<style scoped>
.form {
  padding-left: 26px;
  padding-right: 24px;
  padding-top: 24px;
  background: #fff;
}
.search {
  display: flex;
  position: relative;
}
.search_info {
  width: 192px;
  height: 40px;
  margin-right: 16px;
}
.searchBtn {
  margin-left: 16px;
  height: 36px;
}
.sortStyle {
  height: 19px;
  float: right;
  /* right: 24px; */
  margin-top: 20px;
  margin-bottom: 30px;
}
.pos_right_btn {
  position: absolute;
  right: 0;
}
.opt_font {
  cursor: pointer;
  color: #2e82ff;
}
.detail_title {
  font-size: 16px;
  font-weight: 400;
  color: #212121;
  line-height: 22px;
}
.detail_title span {
  padding-left: 4px;
  background: #1e88e5;
  margin-right: 12px;
}
.row {
  display: flex;
  padding-bottom: 12px;
}
.detail_info {
  padding-top: 16px;
}
.info_left {
  display: flex;
}
.info_right {
  display: flex;
  padding-left: 202px;
}
.info_left_title {
  font-size: 14px;
  font-weight: 400;
  color: #616161;
  line-height: 20px;
  width: 112px;
  margin-right: 16px;
}
.info_left_content {
  font-size: 14px;
  font-weight: 400;
  color: #212121;
  line-height: 20px;
  width: 150px;
}
.pos_top {
  padding-top: 12px;
}
.detailTable {
  margin-top: 16px;
}
.opt_font {
  color: #1e88e5;
  cursor: pointer;
}
.page_pos {
  text-align: right;
  padding-top: 10px;
}
.backBtn {
  text-align: center;
  margin-top: 10px;
}
/deep/ .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* max-height: calc(100% -30px); */
  /* max-width: ; */
}
/deep/ .el-dialog .el-dialog__body {
  flex: 1;
  height: 700px;
  overflow-y: auto;
  padding-top: 0;
}
.tab {
  display: flex;
  position: absolute;
  right: 32px;
  top: 14px;
}
.tab_border {
  border: 1px solid #409eff;
  color: #409eff;
  padding: 10px 12px;
  font-size: 14px;
  margin-right: 16px;
}
.change {
  border: 1px solid #f56c6c;
  color: #f56c6c;
}
.listTable {
  margin-top: 17px;
}
.out {
  position: absolute;
  right: 0;
}
.footer {
  text-align: center;
}
.point {
  cursor: pointer;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #000;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/* 自定义文本按钮的样式 */
.red-text-button {
  color: red; /* 设置文本颜色为红色 */
}
.pannel {
  width: 100%;
  color: #fff;
  border-radius: 4px;
  line-height: 32px;
}
.flex-between {
  justify-content: space-between;
}
.box {
  padding: 6px 16px;
}
.pannel-name {
  font-size: 20px;
}
.pannel-number {
  font-size: 24px;
  /* font-weight: bold; */
}
</style>
